package com.ykdl.member.kid.fittool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.dictionary.DictionaryDataManager;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.beans.BreastFeeding;
import com.ykdl.member.kid.beans.Sleeping;
import com.ykdl.member.kid.beans.StatuResult;
import com.ykdl.member.kid.util.AssertTool;
import com.ykdl.member.kid.util.CalendarUtil;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.views.ArrayWheelAdapter;
import com.ykdl.member.views.DateWheelAdapter;
import com.ykdl.member.views.NumericWheelAdapter;
import com.ykdl.member.views.WheelView;

public class AddSleeping implements OnClickListener{
	
	private Context context;
	private ViewGroup group;
	private RefreshListView mListView;
	private TextView mTxtTitle;
	private TextView mTxtDuration, mTxtTime;
	private Button mBtnClose, mBtnBack, mBtnNext, mBtnComplate;
	private View mLayerDuration, mLayerTime;
	private WheelView mWheelView1, mWheelView2,mWheelView3,mWheelView4,mWheelView5,mWheelView6;
	
	private Sleeping sleeping = new Sleeping();
	private DateWheelAdapter mDateAdapter;
	private CalendarUtil mCalendarUtil;
	private ProgressDialog mProgressDialog;
	
	private OnRefreshListListener mListListener;
	
	public AddSleeping(Context context, ViewGroup group, RefreshListView listview){
		this.context = context;
		this.group = group;
		this.mListView = listview;
		this.mListListener = (OnRefreshListListener)context;
		View view = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_feed_tool_sleep, group);
		
		mCalendarUtil = new CalendarUtil();
		
		mTxtDuration = (TextView) view.findViewById(R.id.txtDuration);
		mTxtTime = (TextView) view.findViewById(R.id.txtTime);
		
		mTxtTitle = (TextView) view.findViewById(R.id.txtTitle);
		mTxtTitle.setText("睡眠时长");
		
		mBtnClose = (Button) view.findViewById(R.id.btnClose);
		mBtnClose.setOnClickListener(this);
		mBtnBack = (Button) view.findViewById(R.id.btnBack);
		mBtnBack.setOnClickListener(this);
		mBtnNext = (Button) view.findViewById(R.id.btnNext);
		mBtnNext.setOnClickListener(this);
		mBtnComplate = (Button) view.findViewById(R.id.btnComplate);
		mBtnComplate.setOnClickListener(this);
		
		
		mLayerDuration = view.findViewById(R.id.layerDuration);
		mWheelView1 = (WheelView) view.findViewById(R.id.wheelView1);
		mWheelView1.setAdapter(new NumericWheelAdapter(0, 23));
		mWheelView1.setLabel("小时");
		mWheelView2 = (WheelView) view.findViewById(R.id.wheelView2);
		mWheelView2.setAdapter(new NumericWheelAdapter(0, 59));
		mWheelView2.setLabel("分钟");
		
		
		mLayerTime = view.findViewById(R.id.layerSleepTime);
		mWheelView3 = (WheelView) view.findViewById(R.id.wheelView3);
		String[] strs = {"入睡时间", "醒来时间"};
		mWheelView3.setAdapter(new ArrayWheelAdapter<String>(strs));
		mWheelView4 = (WheelView) view.findViewById(R.id.wheelView4);
		Calendar end = Calendar.getInstance();
		long time = end.getTimeInMillis();
		mDateAdapter = new DateWheelAdapter(time-(24*60*60*1000*14), time);
		mWheelView4.setAdapter(mDateAdapter);
		mWheelView5 = (WheelView) view.findViewById(R.id.wheelView5);
		mWheelView5.setAdapter(new NumericWheelAdapter(0, 23));
		mWheelView6 = (WheelView) view.findViewById(R.id.wheelView6);
		mWheelView6.setAdapter(new NumericWheelAdapter(0, 59));
		
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnClose:
			group.removeAllViews();
			group.setVisibility(View.GONE);
			mListView.setEnabled(true);
			break;
		case R.id.btnBack:
			if (mLayerTime.getVisibility() == View.VISIBLE) {
				mLayerDuration.setVisibility(View.VISIBLE);
				mLayerTime.setVisibility(View.GONE);
				mBtnComplate.setVisibility(View.GONE);
				mBtnNext.setVisibility(View.VISIBLE);
				mBtnBack.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.VISIBLE);
				mTxtTitle.setText("睡眠时长");
			}
			break;
		case R.id.btnNext:
			if (mLayerDuration.getVisibility() == View.VISIBLE) {
				int item1 = mWheelView1.getCurrentItem();
				int item2 = mWheelView2.getCurrentItem();
				
				int duration = item1*60+item2;
				sleeping.setDuration(duration);
				
				mTxtDuration.setText(item1+"小时"+item2+"分钟");
				mLayerDuration.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.VISIBLE);
				mBtnClose.setVisibility(View.GONE);
				mBtnNext.setVisibility(View.GONE);
				mBtnComplate.setVisibility(View.VISIBLE);
				mLayerTime.setVisibility(View.VISIBLE);
				mTxtTitle.setText("入睡/醒来时间");
			}
			break;
		case R.id.btnComplate:
			if (mLayerTime.getVisibility() == View.VISIBLE) {
				int item3 = mWheelView3.getCurrentItem();
				String month, day;
				String strDate = mDateAdapter.getItem(mWheelView4.getCurrentItem());
				if (strDate.equals("今天")) {
					String[] str = new SimpleDateFormat("MM-dd").format(new Date()).split("-");
					month = str[0];
					day = str[1];
				} else {
					String[] str = strDate.split("月");
					month = str[0];
					day = str[1].replace("日", "");
				}
				int hour = mWheelView5.getCurrentItem();
				int minute = mWheelView6.getCurrentItem();
				long time = mCalendarUtil.getLongByString(month, day, String.valueOf(hour), String.valueOf(minute));
				if (item3 == 0) {
					sleeping.setBegin_time(time/1000);
					sleeping.setEnd_time(time/1000+sleeping.getDuration()*60);
				} else {
					sleeping.setEnd_time(time/1000);
					sleeping.setBegin_time(time/1000 - sleeping.getDuration()*60);
				}
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				mTxtTime.setText(sdf.format(new Date(time)));
				submitData(sleeping);
			}
			break;
		default:
			break;
		}
	}
	
	private void submitData(Sleeping sleep) {
		mProgressDialog = ProgressDialog.show(context, null, "正在提交...");
		Network.getInstance().addFeedBean(sleep, new AddDataTag());
	}
	
	private class AddDataTag implements ITag<StatuResult>{

		@Override
		public void refreshUI(StatuResult t, DataState ds) {
			mProgressDialog.dismiss();
			if (t != null) {
				// 是否刷新视图 ？？
				group.removeAllViews();
				group.setVisibility(View.GONE);
				mListView.setEnabled(true);
				mListListener.refreshList();
			} else {
				Toast.makeText(context, "添加数据失败", 1).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			mProgressDialog.dismiss();
			Toast.makeText(context, "添加数据失败", 1).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
}
